package entity;

import java.util.ArrayList;

/**
 * An ICalendar object with fields defined in RFFC 5445.
 * ICalendar objects can be composed of multiple Event objects.
 * 
 * @author Sean Young
 * @version 1.0
 *
 */
public class ICalendar implements EntityConstants{
	
	private ArrayList<Event> events;
	private TimeZoneIdentifier tZID;
	private Version version;
	
	/** The name used to identify a particular ICalendar while navigating the interface */
	private String name;
	
	/**
     * Constructor for the ICalendar object. Sets the name of the 
     * ICalendar to the value specified in the parameter. The timezone
     * is set to the default timezone.
     * @param name The name of the ICalendar object.
     */
	public ICalendar(String name) {
		this.name = name;
		events = new ArrayList<Event>();
		tZID = new TimeZoneIdentifier();
		version = new Version();
	}
	
	/**
	 * Sets the TZID for this ICalendar 
	 * @param tzid The Time Zone Identifier
	 */
	public void setTZID(String tzid) {
		this.tZID.setTzid(tzid);
	}
	
	/**
	 * Sets the Version for this ICalendar
	 * @param version THe Version
	 */
	public void setVersion(String version) {
		this.version.setVerValue(version);
	}
	
	/**
	 * Adds an Event to this ICalendar
	 * @param e The Event to add
	 */
	public void addEvent(Event e) {
		events.add(e);
	}
	
	
	public String getName() {
		return this.name;
	}
	
	/**
	 * Returns a string containing the values of the fields of the
	 * ICalendar as well as any events associated with it. Values for
	 * PRODID and METHOD are defined in EntityConstants.
	 */
	public String toString() {
		StringBuilder calendarString = new StringBuilder("BEGIN:VCALENDAR\r\n");
		
		calendarString.append("PRODID:");
		calendarString.append(PRODID);
		calendarString.append("\r\n");
		
		calendarString.append(this.version.toString());
		
		calendarString.append("METHOD:");
		calendarString.append(METHOD);
		calendarString.append("\r\n");
		
		calendarString.append("BEGIN:VTIMEZONE\r\n");
		calendarString.append(this.tZID.toString());
		calendarString.append("END:VTIMEZONE\r\n");
		
		for(Event e: events) {
			calendarString.append(e.toString());
		}
		
		calendarString.append("END:VCALENDAR");
		return calendarString.toString();
	}
}
